package com.project.poetry.service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.poetry.common.anno.CanOperation;
import com.project.poetry.service.entity.dto.author.AuthorDTO;
import com.project.poetry.service.entity.dto.author.AuthorDetailDTO;
import com.project.poetry.service.entity.dto.author.AuthorListDTO;
import com.project.poetry.service.entity.dto.author.AuthorListOutDTO;
import com.project.poetry.service.entity.po.author.AuthorPO;
import com.project.poetry.service.service.AuthorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 作者控制器
 * @author mengxj
 */
@Api("作者")
@RestController
@RequestMapping("author")
public class AuthorController {

    @Resource
    private AuthorService authorService;

    @ApiOperation("根据条件查询作者列表")
    @GetMapping("authorList")
    public IPage<AuthorListOutDTO> authorList(AuthorListDTO authorList){
        return this.authorService.authorList(authorList);
    }

    @CanOperation("保存作者信息")
    @ApiOperation("保存作者信息")
    @PostMapping("saveAuthor")
    public void saveAuthor(@RequestBody AuthorDTO author){
        this.authorService.saveAuthor(author);
    }

    @ApiOperation("获取朝代和作者列表的对应关系")
    @GetMapping("getAuthorAndDynasty")
    public Map<String, List<AuthorPO>> getAuthorAndDynasty(){
        return this.authorService.getAuthorAndDynasty();
    }

    @CanOperation("删除作者")
    @ApiOperation("根据id删除作者")
    @DeleteMapping("deleteById/{id}")
    public void deleteById(@PathVariable("id") Long id){
        this.authorService.deleteById(id);
    }

    @CanOperation("编辑作者")
    @ApiOperation("根据作者id查询查询详情，用于跳转编辑页")
    @GetMapping("selectById/{id}")
    public AuthorPO selectById(@PathVariable("id") Long id){
        return this.authorService.getById(id);
    }

    @ApiOperation("根据作者id查询详情，用于跳转详情页")
    @GetMapping("authorDetailById/{authorId}")
    public AuthorDetailDTO authorDetailById(@PathVariable("authorId") Long authorId){
        return this.authorService.authorDetailById(authorId);
    }
}
